<?php
namespace app\index\model;
use think\Model;
use app\index\model\Myssql;

// include_once '/general/erp4/service/order.ser.php';
// include_once '/general/erp4/service/orderNum.ser.php';



class Customer extends Model{

    public $conn;
    private $db;
    private  $host;
    private  $pass;
    private $tabName='Customer';
    public function __construct()
    {
        $config = db('config')->where(['zhangTao'=>'JX'])->find();
        if (!empty($config)) {
            $this->host = $config['ip'];
            if (!empty($db)) {
                $this->db = $db;
            } else {
                $this->db = $config['db1'];
            }
            $this->pass = $config['pass'];
        } else {
            throw Exception('配置项没有找到');
        }
        $this->conn = new Myssql($this->host, $this->db, $this->pass);
    }
    /**
     * 获取数据
     */
    public function getList($pars=null){
        $page = isset($pars['page']) ? $pars['page'] : 1;
        $limit = isset($pars['limit']) ? $pars['limit'] : 10;
        $sql = " 1=1  ";
        if(!empty($pars['cCusName'])){
            $sql .= " and (cCusName like '%{$pars['cCusName']}%' or cCusAbbName like '%{$pars['cCusName']}%') ";
        }
        if(!empty($pars['cCCCode'])){
            $sql .= " and (cCCCode = '{$pars['cCCCode']}') ";
        }
        if(!empty($pars['cCusName'])){            
            $data = $this->conn->selectRaw($this->tabName,' top 10 * ',$sql);
            if(count($data) > 0){
                foreach ($data as $key=>$val){
                    $data[$key] = $this->setvo($val);
                }
            }
            return array(
                'data'=>$data
                ,'count'=>10
            );
        }else{
            $data = $this->conn->queryWithPage($pars['table'],' * ', $sql , 'cCusCode', null, $page, $limit);
            if(count($data['data']) > 0){
                foreach ($data['data'] as $key=>$val){
                    $data['data'][$key] = $this->setvo($val);
                }
            }
            return array(
                'data'=>$data['data']
                ,'count'=>$data['count']
            );
        }

    }

    /**
     * @param $data
     */
    public function setvo($data){
        if(count($data) > 0){
            $cCusPPerson = $data['cCusPPerson'];
            $temp = $this->conn->selectOne('Person','cPersonName'," cPersonCode='$cCusPPerson' ");
            if(is_array($temp)){
                $data['cPersonName'] = isset($temp[0]['cPersonName']) ? $temp[0]['cPersonName'] : '';
            }
            $temp = $this->conn->selectOne('CustomerClass','cCCName',"cCCCode='{$data['cCCCode']}'");
            if(is_array($temp)){
                $data['cCCName'] = isset($temp[0]['cCCName']) ? $temp[0]['cCCName'] : '';
            }
        }
        return $data;
    }

    public function countryList(){
        $temp = $this->conn->selectRaw('CustomerClass','cCCCode,cCCName',"1=1 and bCCEnd=1");
        return $temp;
        // if(is_array($temp)){
        //     foreach($temp as $row){
        //         echo "<option value='{$row['cCCCode']}'>{$row['cCCName']}</option>";
        //     }
        // }
    }

    public function getCustomer($cCusCode){
        $temp = $this->conn->selectOne('Customer','cCCCode,cCusAbbName,cCusName,cCusAddress,cMemo',"cCusCode='$cCusCode'");
        if(!empty($temp)){
            $customerClass = $this->conn->selectOne('CustomerClass','cCCName',"cCCCode='{$temp[0]['cCCCode']}'");
            $temp[0]["cCCName"]=!empty($customerClass[0]["cCCName"])?$customerClass[0]["cCCName"]:"";
            return $temp;
        }
        return $temp;
    }

    // /**
    //  * 
    //  */
    // public function getcCusName($cCusCode){
    //     $temp = $this->msdb->select_one($this->tabName,'cCusName'," cCusCode='{$cCusCode}' ");
    //     if(is_array($temp)){
    //         return $temp['cCusName'];
    //     }
    // }

    // /**
    //  * 
    //  */
    // public function getcCusAbbName($cCusCode){
    //     $temp = $this->msdb->select_one($this->tabName,'cCusAbbName'," cCusCode='{$cCusCode}' ");
    //     if(is_array($temp)){
    //         return $temp['cCusAbbName'];
    //     }
    // }
    
    // /**
    //  * 
    //  */
    // public function getcCusAddress($cCusCode){
    //     $temp = $this->msdb->select_one($this->tabName,'cCusAddress'," cCusCode='{$cCusCode}' ");
    //     if(is_array($temp)){
    //         return $temp['cCusAddress'];
    //     }
    // }
    
    //  /**
    //  * 
    //  */
    // public function getcCusInfo($cCusCode){
    //     $temp = $this->msdb->select_one($this->tabName,'cCCCode,cCusAbbName,cCusName,cCusAddress,cMemo'," cCusCode='{$cCusCode}' ");
    //     if(is_array($temp)){
    //         $customerClass = $this->msdb->select_one('CustomerClass','cCCName',"cCCCode='{$temp['cCCCode']}'");
    //         $temp["cCCName"]=!empty($customerClass["cCCName"])?$customerClass["cCCName"]:"";
    //         return $temp;
    //     }
    // }
    
    


    // /**
    //  * 锟斤拷取锟酵伙拷锟斤拷锟斤拷
    //  */
    // public function getcCCName($cCusCode){
    //     $temp = $this->msdb->select_one($this->tabName,'cCCCode',"cCusCode='{$cCusCode}'");

    //     $temp = $this->msdb->select_one('CustomerClass','cCCName',"cCCCode='{$temp['cCCCode']}'");
    //     if(is_array($temp)){
    //         return $temp['cCCName'];
    //     }
    // }

    // /**
    //  * 锟斤拷锟捷癸拷锟揭伙拷取锟酵伙拷锟斤拷锟斤拷
    //  */
    // public function getcCodeByGj($cCCCode)
    // {
    //     $sql = "
    //         select a.cCusCode from Customer a left join CustomerClass b on b.cCCCode = a.cCCCode where b.bCCEnd = '1' and  b.cCCCode = '$cCCCode'
    //     ";
    //     $res = $this->msdb->query($sql);
    //     $cCusCodes = '';
    //     while ($row = sqlsrv_fetch_array($res)){
    //         $cCusCodes .= $row['cCusCode'].',';
    //     }
    //     return $cCusCodes;
    // }

    // /**
    //  * 锟斤拷取锟酵伙拷锟斤拷锟斤拷协锟斤拷
    //  */
    // public function getcName($cCusCode,$orderType){
    //     if($orderType == 'NX'){
    //         $sql = "
    //         select b.cName from Customer a 
    //         left join AA_Agreement b on b.cCode = a.cCusSAProtocol 
    //         where cCusCode = '$cCusCode' 
    //         ";
    //     }
    //     else{
    //         $sql = "
    //         select b.cName from Customer a 
    //         left join AA_Agreement b on b.cCode = a.cCusExProtocol 
    //         where cCusCode = '$cCusCode' 
    //         ";
    //     }
    //     $temp = $this->msdb->query($sql);
    //     $row = sqlsrv_fetch_array($temp);
        
    //     if(is_array($row)){
    //         return $row['cName'];
    //     }
    // }
    
    // /**
    //  * 锟斤拷取锟街匡拷锟叫憋拷
    //  */
    // public function getWarehouse($where,$page=1,$limit=10,$zt=110){
    //     if($zt == 'YN'){
    //         $u8Info = $this->db->select_one('dev_config','*',"zhangTao='$zt'");
    //         if(is_array($u8Info)){
    //             $this->msdb = new mssqlDB($u8Info['ip'],array('Database'=>$u8Info['db1'],'UID'=>'sa','PWD'=>$u8Info['pass']));
    //         }
    //     }
    //     $dataInfo = $this->msdb->select_raw_page("Warehouse","*",$where,"cWhCode",null,$page,$limit);
    //     if(!empty($dataInfo["data"])){
    //         foreach ($dataInfo["data"] as $key=>$val){
    //             $dataInfo["data"][$key]["id"]=$val["cWhCode"];
    //         }
    //     }
    //     return $dataInfo;
    // }
    
    
    //  /**
    //  * 锟斤拷取锟街匡拷锟叫憋拷
    //  */
    // public function getSettleType($where,$page=1,$limit=10){ 
    //     $dataInfo = $this->msdb->select_raw_page("SettleStyle","cSSCode as value,cSSName as name",$where,"cSSCode",null,$page,$limit);
    //     if(!empty($dataInfo["data"])){
    //         foreach ($dataInfo["data"] as $key=>$val){
    //             $dataInfo["data"][$key]["id"]=$val["cSSCode"];
    //         }
    //     }
    //     return $dataInfo;
    // }
    
    // /**
    //  * 锟斤拷取锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
    //  */
    // public function getDeliverData($params){
    //     if(empty($params["sales_num"])) return false;
    //     $dataInfo = $this->msdb->select_one("ex_consignment","dgatheringdate,cverifier"," ccode='{$params["sales_num"]}' ");
    //     return $dataInfo;
    // }


}



?>